<template>
  <div id="task">
    <van-tabs v-model="active" color="#87CEEB" @click="changeActive($event)">
      <van-tab title="待处理">
          <form action="/">
          <van-search show-action shape="round" @click-left-icon="onSelect" :clearable=false  v-model="workDescribe" @search="onSelect"  @cancel="onCancel" placeholder="请输入任务描述" />
          </form>
          <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell-group
              v-for="item in list"
              :key="item.id"
            >
            <van-cell
              :title="item.work_class_name"
              border:true
              :label="item.work_describe"
              @click.native="onCellClick($event, item)"
              class="van_cell"
            >
             <!-- <span >{{item.create_at}}</span> -->
              <template v-if="item.work_image_url">
                <van-image  class='create_class' v-for="info in item.work_image_url" :key="info.id"
                    width="0.8rem"
                    height="0.8rem"
                    fit=""
                    :src="info"
                  />
              </template>
                <!-- 使用 right-icon 插槽来自定义右侧图标 -->
                <template #right-icon>
                <!-- <van-button type="" size="mini" @click="PreViewRow(item)">任务</van-button> -->
                </template>
              </van-cell>
            </van-cell-group>
          </van-list>
          </van-pull-refresh>
      </van-tab>
      <van-tab title="处理中">
          <form action="/">
          <van-search show-action shape="round" @click-left-icon="onSelect" :clearable=false  v-model="workDescribe" @search="onSelect"  @cancel="onCancel" placeholder="请输入任务描述" />
          </form>
          <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell-group
              v-for="item in list"
              :key="item.id"
            >
            <van-cell
              :title="item.work_class_name"
              border:true
              :label="item.work_describe"
              @click.native="onCellClick($event, item)"
              class="van_cell"
            >
              <template v-if="item.work_image_url">
                <van-image  class='create_class' v-for="info in item.work_image_url" :key="info.id"
                    width="0.8rem"
                    height="0.8rem"
                    fit=""
                    :src="info"
                  />
              </template>
                <!-- 使用 right-icon 插槽来自定义右侧图标 -->
                <template #right-icon>
                  <!-- <van-button type="" size="mini" @click="PreViewRow(item)">任务</van-button> -->
                </template>
              </van-cell>
            </van-cell-group>
          </van-list>
          </van-pull-refresh>
      </van-tab>
      <van-tab title="已完结">
          <form action="/">
          <van-search show-action shape="round" @click-left-icon="onSelect" :clearable=false  v-model="workDescribe" @search="onSelect"  @cancel="onCancel" placeholder="请输入任务描述" />
          </form>
          <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell-group
              v-for="item in list"
              :key="item.id"
            >
            <van-cell
              :title="item.work_class_name"
              border:true
              :label="item.work_describe"
              @click.native="onCellClick($event, item)"
              class="van_cell"
            >

              <template v-if="item.work_image_url">
                <van-image  class='create_class' v-for="info in item.work_image_url" :key="info.id"
                    width="0.8rem"
                    height="0.8rem"
                    fit=""
                    :src="info"
                  />
              </template>
                <!-- 使用 right-icon 插槽来自定义右侧图标 -->
                <template #right-icon>
                  <!-- <van-button type="" size="mini" @click="PreViewRow(item)">任务</van-button> -->
                </template>
              </van-cell>
            </van-cell-group>
          </van-list>
          </van-pull-refresh>
      </van-tab>
      <!-- <van-tab title="标签 4">内容 4</van-tab> -->
    </van-tabs>
  </div>
</template>

<script>
import { taskList } from '@/api/task.js'
import { getItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      page: 1,
      limit: 20,
      workDescribe: '',
      systemUserId: '',
      active: 0
    }
  },
  methods: {
    async onLoad () {
      this.systemUserId = getItem('userInfo').id
      // 异步更新数据
      this.loading = true
      const res = await taskList(this.page, this.limit, this.workDescribe, this.systemUserId, this.active)
      if (res.data.code === 1) {
        this.page += 1
      }
      const arr = res.data.data
      this.list = [...this.list, ...arr]
      this.loading = false
      if (arr.length === 0) {
        this.finished = true
      }
    },
    async onSelect (val) {
      // 异步更新数据
      this.loading = true
      const res = await taskList(1, 10, this.workDescribe)
      if (res.data.code === 1) {
        this.list = res.data.data
        this.finished = true
        this.loading = false
      }
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
      this.loading = false
      this.$toast.success('刷新成功！')
    },
    PreViewRow (item) {
      this.$router.push('/question/addWork?id=' + item.id)
    },
    PreDetailsRow (item) {
      this.$router.push('/question/details?id=' + item.group_id)
    },
    onCancel () {
      this.workDescribe = ''
      this.page = 1
      this.limit = 20
      this.finished = false
      this.refreshing = false
      this.onLoad()
    },
    onCellClick (event, item) {
      // 判断点击的是否是title
      const titleElement = event.target.closest('.van-cell__title')
      if (titleElement) {
        // 在这里处理title的点击事件
        this.$router.push('/task/details?id=' + item.id)
      }
    },
    changeActive () {
      this.page = 1
      this.list = []
      this.onLoad()
    }
  }
}
</script>

<style>
  .create_class{
   margin-right:8px;
 }
</style>
